package com.jie.other.jetpack.paging

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import com.jie.commonlib.widget.recyclerview.BindingViewHolder
import com.jie.other.databinding.OtherAdapterPagingListBinding
import com.jie.other.jetpack.paging.bean.Repo


class PagingAdapter : PagingDataAdapter<Repo, BindingViewHolder<OtherAdapterPagingListBinding>>(
    object : DiffUtil.ItemCallback<Repo>() {
        override fun areItemsTheSame(oldItem: Repo, newItem: Repo): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Repo, newItem: Repo): Boolean {
            return oldItem == newItem
        }
    }) {

    // 列表点击事件
    var mItemClick : ((item : Repo?) -> Unit)? = null

    override fun onBindViewHolder(
        holder: BindingViewHolder<OtherAdapterPagingListBinding>,
        position: Int
    ) {
        val repo = getItem(position)
        holder.viewBinding.run {
            tvName.text = "${repo?.stars}: ${repo?.fullName}"
            tvDesc.text = repo?.description
            clItem.setOnClickListener {
                mItemClick?.invoke(repo)
            }
        }

    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<OtherAdapterPagingListBinding> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return BindingViewHolder(
            OtherAdapterPagingListBinding.inflate(layoutInflater, parent, false)
        )
    }

}